package com.hucii.hc_connectivity;

import android.Manifest;
import android.Manifest.permission;
import android.app.Activity;
import android.content.pm.PackageManager;

import androidx.annotation.VisibleForTesting;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;


final class ConnectPermissions {
    interface PermissionsRegistry {
        @SuppressWarnings("deprecation")
        void addListener(
                io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener handler);
    }

    interface ResultCallback {
        void onResult(String errorCode, String errorDescription);
    }

    private static final int PERMISSIONS_REQUEST_CODE_ACCESS_FINE_LOCATION = 127;
    private static final int PERMISSIONS_REQUEST_CODE_LOCATION_HARDWARE = 656889659;
    private boolean ongoing = false;

    void requestPermissions(
            Activity activity,
            PermissionsRegistry permissionsRegistry,
            ResultCallback callback) {
        if (ongoing) {
            callback.onResult("coonectPermission", "Connect permission request ongoing");
        }
        if (!hasAccessFineLocationPermission(activity) || !hasLocationPermission(activity)) {
            permissionsRegistry.addListener(
                    new ConnectRequestPermissionsListener(activity,
                            (String errorCode, String errorDescription) -> {
                                ongoing = false;
                                callback.onResult(errorCode, errorDescription);
                            }));
            ongoing = true;
            ActivityCompat.requestPermissions(
                    activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                    PERMISSIONS_REQUEST_CODE_ACCESS_FINE_LOCATION);
        } else {
            // Permissions already exist. Call the callback with success.
            callback.onResult(null, null);
        }
    }

    private boolean hasAccessFineLocationPermission(Activity activity) {
        return ContextCompat.checkSelfPermission(activity, permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED;
    }

    private boolean hasLocationPermission(Activity activity) {
        return ContextCompat.checkSelfPermission(activity, permission.ACCESS_COARSE_LOCATION)
                == PackageManager.PERMISSION_GRANTED;
    }

    @VisibleForTesting
    @SuppressWarnings("deprecation")
    static final class ConnectRequestPermissionsListener
            implements io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener {

        // There's no way to unregister permission listeners in the v1 embedding, so we'll be called
        // duplicate times in cases where the user denies and then grants a permission. Keep track of if
        // we've responded before and bail out of handling the callback manually if this is a repeat
        // call.
        boolean alreadyCalled = false;

        final ResultCallback callback;
        final Activity activity;

        @VisibleForTesting
        ConnectRequestPermissionsListener(Activity activity, ResultCallback callback) {
            this.callback = callback;
            this.activity = activity;
        }

        @Override
        public boolean onRequestPermissionsResult(int id, String[] permissions, int[] grantResults) {
            if (alreadyCalled || (id != PERMISSIONS_REQUEST_CODE_ACCESS_FINE_LOCATION && id != PERMISSIONS_REQUEST_CODE_LOCATION_HARDWARE)) {
                return false;
            }
//
            alreadyCalled = true;
//            if (grantResults.length > 0) {
//                if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
//                    ActivityCompat.requestPermissions(
//                            activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
//                            PERMISSIONS_REQUEST_CODE_ACCESS_FINE_LOCATION);
//                } else if (grantResults.length > 1 && grantResults[1] != PackageManager.PERMISSION_GRANTED) {
//                    ActivityCompat.requestPermissions(
//                            activity, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
//                            PERMISSIONS_REQUEST_CODE_LOCATION_HARDWARE);
//                } else {
//                    callback.onResult(null, null);
//                }
//            }

            callback.onResult(null, null);
            return true;
        }
    }
}
